using System;
using System.Collections.Generic;
using System.Linq;
using Ninject;
using sdm.exam.domain.Abstract;
using sdm.exam.domain.Entities;

namespace sdm.exam.domain.DataContext
{
    public class SemesterClassDomain
    {
        private readonly ISdmExamRepository _repository = Ninjector.Kernel.Get<ISdmExamRepository>();

        /// <summary>
        /// Gets a list of semester classes.
        /// </summary>
        /// <returns>The list of semester classes.</returns>
        public List<Entities.SemesterClass> GetListOfSemesterClasses()
        {
            var q = from item in _repository.SemesterClasses select item;

            return q.ToList();
        }

        /// <summary>
        /// Adds a new semester class to the database
        /// </summary>
        /// <param name="name">The name of the semester class. For example: CS2010.</param>
        /// <param name="startDate">The date when the semester class is started.</param>
        /// <param name="endDate">The date when the semester class has finished its education.</param>
        /// <param name="education">The education, as the semester class is part of.</param>
        public void AddNewSemesterClass(string name, DateTime startDate, DateTime endDate, Education education)
        {
            var semesterClass = new SemesterClass() {Name = name, StartDate = startDate, EndDate = endDate, Education = education};
            _repository.SemesterClasses.Add(semesterClass);
            _repository.SaveChanges();
        }

        /// <summary>
        /// Adds a new semester class to the database
        /// </summary>
        /// <param name="semesterClass">The semesterclass to add.</param>
        public void AddNewSemesterClass(SemesterClass semesterClass)
        {
            _repository.SemesterClasses.Add(semesterClass);
            _repository.SaveChanges();
        }

        /// <summary>
        /// Edit the semesterclass by id
        /// </summary>
        /// <param name="id">The id to lookup on, to get the specific semesterclass.</param>
        /// <param name="newName">The new name of the semesterclass</param>
        /// <param name="newStartDate">The new start date of the semester class</param>
        /// <param name="newEndDate">The new end date of the semester class</param>
        /// <param name="newEducation">The new education.</param>
        public void EddSemesterClass(int id, string newName, DateTime newStartDate, DateTime newEndDate, Education newEducation)
        {

            var list = GetListOfSemesterClasses();

            SemesterClass semesterClass = _repository.SemesterClasses.FirstOrDefault(item => item.Id == id);

            if (semesterClass != null)
            {
                semesterClass.Name = newName;
                semesterClass.StartDate = newStartDate;
                semesterClass.EndDate = newEndDate;
                semesterClass.Education = newEducation;
            }

            _repository.SaveChanges();
        }
        /// <summary>
        /// Delete a specific semester class.
        /// </summary>
        /// <param name="id">The id of the semesterclass to delete.</param>
        public void DeleteSemesterClass(int id)
        {
            _repository.SemesterClasses.Remove(_repository.SemesterClasses.FirstOrDefault(m => m.Id == id));
            _repository.SaveChanges();
        }

        /// <summary>
        /// Adds a studyplan to a semesterClass
        /// </summary>
        /// <param name="semesterClass"></param>
        /// <param name="studyplan"></param>
        public void AddStudyplanToSemesterClass(SemesterClass semesterClass, Studyplan studyplan)
        {
            SemesterClass semesterClassModifier = _repository.SemesterClasses.FirstOrDefault(m => m.Id == semesterClass.Id);
            Studyplan studyPlan = _repository.Studyplans.FirstOrDefault(m => m.Id == studyplan.Id);
            if (studyPlan != null)
            {
                studyPlan.IsAssignedToSemesterClass = true;
                studyPlan.SemesterClass = semesterClassModifier;
            }
            _repository.SaveChanges();
        }
    }
}
